package fact.view.controller;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import fact.view.DTO.RoleDTO;
import fact.view.DTO.SegmentDTO;
import fact.view.model.SegmentModelBean;
import fact.view.util.FacesUtils;
import fact.view.util.ServletUtil;


@ManagedBean(name = "segmentBean")
@RequestScoped
public class SegmentBean extends ServletUtil implements Serializable {

	private static final long serialVersionUID = 8292853620260831727L;
	private boolean opened = false;
	private boolean editOpened = false;
	
	private SelectItem[] segmentSelectList;
	
	
	@ManagedProperty(value="#{segmentModelBean}")
	private SegmentModelBean segmentModelBean;
	
	ArrayList<SegmentDTO> segmentList=new  ArrayList<SegmentDTO>();

	private SegmentDTO segment=new SegmentDTO();
	private SegmentDTO editSegment=new SegmentDTO();
		 
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		
	public String addSegment(ActionEvent event) throws SQLException
	{
		opened=false;
		editOpened=false;
		
		if(this.segment.getSegmentName()==null || this.segment.getSegmentName().trim().equalsIgnoreCase(""))
		{
			opened=true;
			
			FacesUtils.addErrorMessage("form:segmentName","Segment Name cannot be empty.");
		}
		
		
		if(this.segment.getPosition()==0)
		{
			opened=true;			
			FacesUtils.addErrorMessage("form:segmentPosition","Segment Position should be a number and cannot be zero.");
		}
		
		
		if(opened==false)
		{
			if(segmentModelBean.getSegment(this.segment.getSegmentName().trim())!=null)
			{
				opened=true;
				
				FacesUtils.addErrorMessage("form:segmentName","Segment Name Already Exist.");
			}
			else
			{
				if(segmentModelBean.addSegment(this.segment.getSegmentName(),this.segment.getPosition())==true)
				{
					segmentList=segmentModelBean.getSegmentList();
					session.setAttribute("session_segmentList", segmentList);
					
					FacesUtils.addInfoMessage(null,"Segment has successfully been added.");
					return "admin/segment.jsf";
	
				}
				
			}
		}
		return null;
	}
	
	public String deleteSegment() throws SQLException
	{
		System.out.println("Segment Id :"+this.segment.getSegmentId());
		if(segmentModelBean.deleteSegment(this.segment.getSegmentId())==true)
		{
			segmentList=segmentModelBean.getSegmentList();
			session.setAttribute("session_segmentList", segmentList);
			
			FacesUtils.addInfoMessage(null,"Segment has successfully been deleted.");
		}
		return "admin/segment.jsf";
	}
	
	 
	public String editSegment() throws SQLException
	{
		editOpened=false;
		opened=false;
		
		if(this.editSegment.getSegmentName()==null || this.editSegment.getSegmentName().trim().equalsIgnoreCase(""))
		{
			editOpened=true;
			
			FacesUtils.addErrorMessage("form:editSegmentName","Segment Name cannot be empty.");
		}
		
		
		if(this.editSegment.getPosition()==0)
		{
			editOpened=true;			
			FacesUtils.addErrorMessage("form:editSegmentPosition","Segment Position should be a number and cannot be zero.");
		}
		
		if(editOpened==false)
		{
			System.out.println("Segment Id :"+this.editSegment.getSegmentId());
			if(segmentModelBean.editSegment(this.editSegment.getSegmentName(),this.editSegment.getPosition(),this.editSegment.getSegmentId())==true)
			{
				segmentList=segmentModelBean.getSegmentList();
				session.setAttribute("session_segmentList", segmentList);
				
				FacesUtils.addInfoMessage(null,"Segmment has successfully been edited.");
			}
			return "admin/segment.jsf";
		}
		return null;
	}

	public SegmentModelBean getSegmentModelBean() {
		return segmentModelBean;
	}

	public void setSegmentModelBean(SegmentModelBean segmentModelBean) {
		this.segmentModelBean = segmentModelBean;
	}

	public ArrayList<SegmentDTO> getSegmentList() {
		try{
			
			if(session.getAttribute("session_segmentList")==null)
			{
				segmentList=segmentModelBean.getSegmentList();
				session.setAttribute("session_segmentList", segmentList);
			}
			else
				segmentList=(ArrayList<SegmentDTO>)session.getAttribute("session_segmentList");
		   }
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
	
		return segmentList;
	}
	
	
	public void setSegmentList(ArrayList<SegmentDTO> segmentList) {
		this.segmentList = segmentList;
	}
	
	public SelectItem[] getSegmentSelectList() {
		
		try{
			
			if(session.getAttribute("session_segmentList")==null)
			{
				segmentList=segmentModelBean.getSegmentList();
				session.setAttribute("session_segmentList", segmentList);
			}
			else
				segmentList=(ArrayList<SegmentDTO>)session.getAttribute("session_segmentList");
		   }
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		segmentSelectList = new SelectItem[segmentList.size()];
		 for(int i=0;i<segmentList.size();i++)
			 segmentSelectList[i] = new SelectItem(segmentList.get(i).getSegmentId(), segmentList.get(i).getSegmentName());
		 
		return segmentSelectList;
	}

	public void setSegmentSelectList(SelectItem[] segmentSelectList) {
		this.segmentSelectList = segmentSelectList;
	}
	
	public SegmentDTO getSegment() {
		return segment;
	}

	public void setSegment(SegmentDTO segment) {
		this.segment = segment;
	}

	public SegmentDTO getEditSegment() {
		return editSegment;
	}

	public void setEditSegment(SegmentDTO editSegment) {
		this.editSegment = editSegment;
	}

	/** -- Popup Modal Event ---**/
		public boolean isOpened() { return opened; }
		public boolean isEditOpened() { return editOpened; }
	    
	    public void setOpened(boolean opened) { this.opened = opened; }
	    public void setEditOpened(boolean editOpened) { this.editOpened = editOpened; }
	    
	    
	    public void openEvent(ActionEvent event) {
	    	this.segment=null;
	        opened = true;
	        editOpened = false;
	    }
	    public void openEditEvent(ActionEvent event)  throws SQLException{
	    	this.editSegment=segmentModelBean.getSegment(this.editSegment.getSegmentId());
	    	editOpened = true;
	    	opened = false;
	    }
	    
	    
	    public void closeEvent(ActionEvent event) {
	        opened = false;
	        editOpened = false;
	    }

    /*  End of Modal */
	

}
